package com.atguigu.eduservice.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TowSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author qing
 * @since 2021-07-31
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 添加课程分类
     * @param file
     */
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) throws IOException {
        //1.得到文件输入流
        InputStream in = file.getInputStream();
        //2.读取文件内容 ：：SubjectExcelListener为监听器 为读取核心类
        EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();



    }

    @Override
    public List<OneSubject> geAllOneTwoSubject() {
        //1.查询出所有的一级分类
    QueryWrapper<EduSubject> eduSubjectQueryWrapperOne = new QueryWrapper<>();
        eduSubjectQueryWrapperOne.eq("parent_id","0");
    List<EduSubject> oneEduSubjectList = this.baseMapper.selectList(eduSubjectQueryWrapperOne);

    //2.查询出所有的二级分类
    QueryWrapper<EduSubject> eduSubjectQueryWrapperTwo = new QueryWrapper<>();
        eduSubjectQueryWrapperTwo.ne("parent_id","0"); //eq是等于 ， ne 是不等于
    List<EduSubject> twoEduSubjectList = this.baseMapper.selectList(eduSubjectQueryWrapperTwo);

    //创建list集合，用于存储最终封装的数据
    List<OneSubject> finalSubjectList = new ArrayList<>();

    //3.封装一级分类
        for (EduSubject oneEduSubject : oneEduSubjectList) {

        OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(oneEduSubject.getId());
//            oneSubject.setTitle(oneEduSubject.getTitle());
        BeanUtils.copyProperties(oneEduSubject,oneSubject);
        finalSubjectList.add(oneSubject);
        //在一级分类循环遍历查询所有二级分类
        //创建list集合封装每个一级分类的二级分类
        List<TowSubject> twoFinalSubjectList = new ArrayList<>();
        //遍历二级分类list集合
        //封装二级分类
        for (EduSubject twoEduSubject : twoEduSubjectList) {
            //获取每个二级分类
            //判断二级分类的parentid 是否和 一级分类的 id 一样 如果一样就属于一级分类下
            if (twoEduSubject.getParentId().equals(oneSubject.getId())){
                //把twoEduSubject的值复制到towSubject中去，在把towSubject 放到twoFinalSubjectList中
                TowSubject towSubject = new TowSubject();
                BeanUtils.copyProperties(twoEduSubject,towSubject);
                twoFinalSubjectList.add(towSubject);

            }

        }
        oneSubject.setChildren(twoFinalSubjectList);
    }

        return finalSubjectList;
}
}
